/**
 * 求最小生成树，其定义为生成树边权之和的模
 * 注意到N只有8，边数最多为28，因此在C(28, 7)中进行枚举即可
 */
#include<bits/stdc++.h>
using namespace std;

using llt = long long;

struct ufset_t{

vector<int> father;

void init(int n){father.assign(n+1, 0);for(int i=1;i<=n;++i)father[i] = i;}
int find(int x){return x == father[x] ? x : father[x] = find(father[x]);}
void unite(int x, int y){father[find(y)] = find(x);}

}UF;

struct edge_t{
	int from;
	int to;
	llt w;
};

vector<edge_t> Edges;

llt MOD;
int N, M;
vector<vector<int>> G;

llt proc(){
    vector<int> flag(M, 0);
	for(int i=0;i<N-1;++i){
		flag[M-1-i] = 1;
	}

	vector<int> perm(M, 0);
	for(int i=0;i<M;++i) perm[i] = i;

    llt ans = MOD;
	do{
		bool ok = true;
		llt tmp = 0;
		UF.init(N);
        for(int i=0;i<M;++i)if(flag[i]){
            const auto & e = Edges[i];
			auto u = UF.find(e.from);
			auto v = UF.find(e.to);
			if(u == v){
				ok = false;
				break;
			}
			UF.unite(e.from, e.to);
			tmp = (tmp + e.w) % MOD;
		}
		if(ok){
			ans = min(ans, tmp);
		}
	}while(next_permutation(flag.begin(), flag.end()));
	return ans;
}

int main(){
#ifndef ONLINE_JUDGE
    freopen("z.txt", "r", stdin);
#endif
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> N >> M >> MOD;
	G.assign(N + 1, {});
	for(int i=0;i<M;++i){
		int a, b;
		llt w;
		cin >> a >> b >> w;
		if(a > b) swap(a, b);
		int t = Edges.size();
		G[a].push_back(t);
		G[b].push_back(t);
		Edges.push_back({a, b, w});
	}
	cout << proc() << endl;
 	return 0;
}